namespace _28_改写 {
  class Vehicle {
    private brand: string;
    private year: number;

    static vehicleCount: number = 0;//静态属性，用于跟踪创建的Vehicle实例数量，初始值为0

    constructor(brand: string, year: number) {
      this.brand = brand;
      this.year = year;
      this.validateYear(year); // 调用私有方法，验证年份
      Vehicle.incrementVehicleCount(); // 调用静态方法，增加车辆数量
    }
    // 定义了一个公共方法 getInfo，返回一个包含车辆品牌和年份的字符串。
    public getInfo(): string {
      return `Brand: ${this.brand}, Year: ${this.year}`;
    }
    // 定义了一个私有方法 validateYear，用于验证传入的年份是否大于当前年份。如果是，则抛出一个错误。
    private validateYear(year: number): void {
      const currentYear = new Date().getFullYear();
      if (year > currentYear) {
        throw new Error(`Year cannot be in the future. Current year: ${currentYear}, Provided year: ${year}`);
      }
    }
    
    // 定义了一个静态公共方法 incrementVehicleCount，用于将静态属性 vehicleCount 的值增加 1。
    public static incrementVehicleCount(): void {
      Vehicle.vehicleCount++;
    }
  }

  class Car extends Vehicle {
    private _mileage: number;

    constructor(brand: string, year: number, mileage: number) {
      super(brand, year);
      this._mileage = mileage;
    }

    public get mileage(): number {
      return this._mileage;
    }

    public set mileage(value: number) {
      this._mileage = value;
    }

    public drive(distance: number): void {
      this._mileage += distance;
    }

    public getInfo(): string {
      return `${super.getInfo()}, Mileage: ${this._mileage}`;
    }

    public static compareMileage(car1: Car, car2: Car): Car {
      return car1.mileage > car2.mileage ? car1 : car2;
    }
  }

  // 创建两个 Car 实例
  const car1 = new Car('Toyota', 2020, 10000);
  const car2 = new Car('Honda', 2019, 15000);

  // 增加 car1 的里程数
  car1.drive(5000);

  // 比较两个实例的里程数，并打印里程数较大的车辆的详细信息
  const carWithMoreMileage = Car.compareMileage(car1, car2);
  console.log(carWithMoreMileage.getInfo());

  // 打印当前车辆数量
  console.log(`Total Vehicles: ${Vehicle.vehicleCount}`);



}